﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using CrystalDecisions.Shared;
using DVSProject.Lib;
using DVSProject;

namespace CR_With_MVC.AspNetForms
{
    public partial class aspnetvisitform : System.Web.UI.Page
    {
        ReportRepo RR = new ReportRepo();
        public DataTable  MyProperty { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                bool isValid = true;

                string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();
                string FSRNOText = System.Web.HttpContext.Current.Session["FsrNoText"].ToString();


                //string strMonthshow = System.Web.HttpContext.Current.Session["rptMonthShow"].ToString();
                //string strFromDate = System.Web.HttpContext.Current.Session["rptFromDate"].ToString();
                //// Setting ToDate
                //string strToDate = System.Web.HttpContext.Current.Session["rptToDate"].ToString();
                // Setting Report Data Source     
                //var rptSource = System.Web.HttpContext.Current.Session["rptSource"]

                //DataTable _dt = (DataTable)System.Web.HttpContext.Current.Session["rptSource"];

                if (string.IsNullOrEmpty(strReportName)) // Checking is Report name provided or not
                {
                    isValid = false;
                }


                if (isValid) // If Report Name provided then do other operation
                {
                    DSDVS  dss = new DSDVS ();

                    //DataTable VHD = RR.VisitHD(FSRNOText);

                    DataTable VHD = RR.VisitHD_EXEC(FSRNOText);
                    
                   
                    DataTable VResult = RR.VisitDtlResult(FSRNOText);
                    DataTable VProcedure = RR.VisitDtlProcedure(FSRNOText);
                    DataTable VWashFactor = RR.VisitDtlWashFactor(FSRNOText);
                    DataTable VMechanical = RR.VisitDtlMechanical(FSRNOText);
                    DataTable VMwwDispenser = RR.VisitDtlMwwDispenser(FSRNOText);
                    DataTable VFeeder = RR.VisitDtlFeeder(FSRNOText);
                    DataTable VTraining = RR.VisitDtlTraining(FSRNOText);
                    DataTable VHandSoapDispenser = RR.VisitDtlHandSoapDispenser(FSRNOText);
                    DataTable VServiceSummary = RR.VisitDtlServiceSummary(FSRNOText);

                    VHD.TableName = "dtVisitHD";
                    VResult.TableName = "dtVisitDtlResult";
                    VProcedure.TableName = "dtVisitDtlProcedure";
                    VWashFactor.TableName = "dtVisitDtlWashFactor";
                    VMechanical.TableName = "dtVisitDtlMechanical";
                    VMwwDispenser.TableName = "dtVisitDtlMwwDispenser";
                    VFeeder.TableName = "dtVisitDtlFeeder";
                    VTraining.TableName = "dtVisitDtlTraining";
                    VHandSoapDispenser.TableName = "dtVisitDtlHandSoapDispenser";
                    VServiceSummary.TableName = "dtVisitDtlServiceSummary";


                    //_dt.TableName = "dt_Complain";
                    DataSet ds = new DataSet();
                    ds.Merge(VHD);
                    ds.Merge(VResult);
                    ds.Merge(VProcedure);
                    ds.Merge(VWashFactor);
                    ds.Merge(VMechanical);
                    ds.Merge(VMwwDispenser);
                    ds.Merge(VFeeder);
                    ds.Merge(VTraining);
                    ds.Merge(VHandSoapDispenser);
                    ds.Merge(VServiceSummary);


                    ReportDocument rd = new ReportDocument();
                    string strRptPath = Server.MapPath("~/") + "Rpts//" + strReportName;
                    //Loading Report
                    rd.Load(strRptPath);


                    // Setting report data source
                    if (VHD != null && VHD.Rows.Count > 0)
                    {
                        rd.SetDataSource(ds);

                        rd.Subreports["VResults"].SetDataSource(ds);
                        rd.Subreports["VProcedure"].SetDataSource(ds);
                        rd.Subreports["VWashFactors"].SetDataSource(ds);
                        rd.Subreports["VMachanical"].SetDataSource(ds);
                        rd.Subreports["VMWWFeeder"].SetDataSource(ds);
                        rd.Subreports["VDispenser"].SetDataSource(ds);
                        rd.Subreports["VTranining"].SetDataSource(ds);
                        //rd.Subreports["VHandSoapDispenser"].SetDataSource(ds);
                        //rd.Subreports["VServiceSummary"].SetDataSource(ds);


                        CrystalReportViewer1.ReportSource = rd;

                        ConnectionInfo myConnectionInfo = new ConnectionInfo();
                        myConnectionInfo.ServerName = "61.19.248.21";
                        myConnectionInfo.DatabaseName = "DVS";
                        myConnectionInfo.UserID = "dvsuser";
                        myConnectionInfo.Password = "adminsql";
                        setDBLOGONforREPORT(myConnectionInfo);

                        rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");




                        Session["ReportName"] = "";
                        Session["FsrNoText"] = "";
                        //Session["monthshow"] = "";
                        //Session["rptFromDate"] = "";
                        //Session["rptToDate"] = "";
                        //Session["rptSource"] = "";

                    }
                    else
                    {
                        //Response.Write("<H2>Nothing Found; No Report name found</H2>");

                    }
                }
                else
                {
                   // Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            catch (Exception ex)
            {
                //Response.Write(ex.ToString());
            }
        }
        private void setDBLOGONforREPORT(ConnectionInfo myconnectioninfo)
        {
            TableLogOnInfos mytableloginfos = new TableLogOnInfos();
            mytableloginfos = CrystalReportViewer1.LogOnInfo;
            foreach (TableLogOnInfo myTableLogOnInfo in mytableloginfos)
            {
                myTableLogOnInfo.ConnectionInfo = myconnectioninfo;
            }

        }
    }
}